home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C23 / Nudep.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1020 b   |  53 lines

  1. //: C23:Nudep.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Naked pointers
  7. #include <fstream>
  8. #include <cstdlib>
  9. using namespace std;
  10. ofstream out("nudep.out");
  11.  
  12. class Cat {
  13. public:
  14.   Cat() { out << "Cat()" << endl; }
  15.   ~Cat() { out << "~Cat()" << endl; }
  16. };
  17.  
  18. class Dog {
  19. public:
  20.   void* operator new(size_t sz) {
  21.     out << "allocating a Dog" << endl;
  22.     throw int(47);
  23.   }
  24.   void operator delete(void* p) {
  25.     out << "deallocating a Dog" << endl;
  26.     ::delete p;
  27.   }
  28. };
  29.  
  30. class UseResources {
  31.   Cat* bp;
  32.   Dog* op;
  33. public:
  34.   UseResources(int count = 1) {
  35.     out << "UseResources()" << endl;
  36.     bp = new Cat[count];
  37.     op = new Dog;
  38.   }
  39.   ~UseResources() {
  40.     out << "~UseResources()" << endl;
  41.     delete []bp; // Array delete
  42.     delete op;
  43.   }
  44. };
  45.  
  46. int main() {
  47.   try {
  48.     UseResources ur(3);
  49.   } catch(int) {
  50.     out << "inside handler" << endl;
  51.   }
  52. } ///:~
  53.